package com.chenbei.common.handler.serializer;

import cn.hutool.core.util.StrUtil;
import com.esotericsoftware.kryo.Serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * DomainUrl 序列化
 */
public class DomainUrlSerializer extends JsonSerializer<String> {

    private final String DOMAIN;

    public DomainUrlSerializer(String domain) {
        this.DOMAIN = domain;
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StrUtil.isBlank(s)) {
            jsonGenerator.writeNull();
            return;
        }
        String fillUrl = (s.startsWith("http")) ? s : DOMAIN + s;
        jsonGenerator.writeString(fillUrl);
    }
}
